package activitys;

import android.app.Activity;
import android.location.Criteria;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


import mytool.MyDialog;
import mytool.PrintLog;
import mytool.ShardPreferencesTool;
import mytool.ToastShow;
import tool.ducong.com.mygpsdemo.MyAPP;
import tool.ducong.com.mygpsdemo.R;

/**
 * Created by zerdoor_pc on 2016/3/14.
 */
public class SetTing extends Activity {
    private TextView setting_times, setting_power;
    private EditText setting_ed_times;
    private Button setting_edit, setting_preservation, setting_cancel;
    private LinearLayout setting_power_ll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        findView();
    }

    private void findView() {
        setting_times = (TextView) findViewById(R.id.setting_times);
        setting_power = (TextView) findViewById(R.id.setting_power);
        setting_ed_times = (EditText) findViewById(R.id.setting_ed_times);
        setting_power_ll= (LinearLayout) findViewById(R.id.setting_power_ll);
        setting_edit = (Button) findViewById(R.id.setting_edit);
        setting_preservation = (Button) findViewById(R.id.setting_preservation);
        setting_cancel = (Button) findViewById(R.id.setting_cancel);
        setting_times.setText(MyAPP.time + "");
        setting_ed_times.setText(MyAPP.time + "");
        try {
            PrintLog.i("MYGPSLONG:" + MyAPP.itude.getLongitude() + " ===" + MyAPP.itude.getLatitude());
            PrintLog.i("MYGPSLONG:" + Distance(104.060637, 30.570872, MyAPP.itude.getLongitude(), MyAPP.itude.getLatitude()));
        }catch (Exception e){}

        if (MyAPP.POWER == Criteria.POWER_MEDIUM) {
            setting_power.setText("中");
        }
        switch (MyAPP.POWER){
            case Criteria.POWER_MEDIUM:
                setting_power.setText("中");
                break;
            case Criteria.POWER_LOW:
                setting_power.setText("低");
                break;
            case Criteria.POWER_HIGH:
                setting_power.setText("高");
                break;

        }
        setting_power_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDialog.showCustomDialog(SetTing.this,setting_power);
            }
        });

    }


    public void edit(View v) {
        if (v == setting_edit) {
            setting_times.setVisibility(View.GONE);
            setting_ed_times.setVisibility(View.VISIBLE);
            setting_edit.setVisibility(View.GONE);
            setting_preservation.setVisibility(View.VISIBLE);
            setting_cancel.setVisibility(View.VISIBLE);
        } else if (v == setting_preservation) {
            String numberedit = String.valueOf(setting_ed_times.getText());

            if (numberedit.length() != 0) {
                try {
                    MyAPP.time = Integer.parseInt(numberedit);
                    ShardPreferencesTool.saveshare(this,"update_time",MyAPP.time);
                    setting_times.setText(MyAPP.time + "");
                    setting_ed_times.setText(MyAPP.time + "");
                    MainActivity.gps_tool.setTimes();
                    cancel(null);
                } catch (Exception e) {
                    ToastShow.showShort(this, "数据格式输入有误！");
                }

            } else {
                ToastShow.showShort(this, "数据不能为空！");
            }
        }
    }

    public void cancel(View v) {
        setting_times.setVisibility(View.VISIBLE);
        setting_ed_times.setVisibility(View.GONE);
        setting_edit.setVisibility(View.VISIBLE);
        setting_preservation.setVisibility(View.GONE);
        setting_cancel.setVisibility(View.GONE);
        closrThisInputMethodManager();
    }

    private void closrThisInputMethodManager() {
        try {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
        }
    }


    public static double Distance(double long1, double lat1, double long2,
                                  double lat2) {
        double a, b, R;
        R = 6378137; // 地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2
                * R
                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)
                * Math.cos(lat2) * sb2 * sb2));
        return d;
    }
}
